#!/usr/bin/python3
# coding=utf-8
import sys
import json


def debug_log(msg):
    log_file = "/tmp/.file.txt"
    fd = open(log_file, 'w+')
    fd.write(str(msg))
    fd.close()


def filecache_result(raw):
    postprocess_result = {
        "code": 0,
        "err_msg": "",
        "result": {}
    }
    rawdata = json.loads(raw)
    cache = {"podmem": {"data": []}}
    cache_type = rawdata['type']
    podid = cache_type
    data = rawdata['data']
    cache['summary'] = "success"
    for key, _value in data.items():
        for value in _value:
            if len(value['sort_file']) == 0:
                continue
            sort_file = value['sort_file']
            i = 0
            for files in sort_file:
                row = {}
                row['key'] = i
                if 'id' in value.keys():
                    row['POD'] = key
                    row['Container'] = value["id"]
                row['Filename'] = files['file']
                row['Filesize'] = files['size']
                row['Cached'] = files['cached']
                row['ActiveCached'] = files['active']
                row['InActiveCached'] = files['inactive']
                ext = 's:'+str(files['shmem']) + ',d:'+str(files['delete'])
                row['ext'] = ext
                i = i + 1
                cache['podmem']['data'].append(row)
    postprocess_result['result'] = cache
    print(json.dumps(postprocess_result, ensure_ascii=False))


def extract_params():
    path, res, task_id = sys.argv[1], "", sys.argv[2]
    with open(path, 'r') as tmp:
        res = tmp.read()
    return res, task_id


if __name__ == "__main__":
    res, _ = extract_params()
    filecache_result(res)
